package com.skydroid.demo.ui.activity

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.skydroid.demo.R
import com.skydroid.demo.helper.LiveWindowHelper
import com.skydroid.demo.listener.VideoListener
import com.skydroid.demo.ui.widget.MyVideoView
import com.skydroid.demo.utils.MEIZU
import com.skydroid.demo.utils.MIUI
import com.skydroid.demo.utils.PermissionUtils
import tv.danmaku.ijk.media.player.IMediaPlayer
import java.io.IOException

class LiveWindowActivity:AppCompatActivity(), VideoListener {
    private var videoView: MyVideoView? = null

    private var mContext: Context? = null
    private var mediacodec: Int? = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_live_window)
        this.mContext = this
        parseIntent()
        initView()
    }

    companion object{
        fun start(context:Context,mediacodec:Int){
            var intent = Intent(context,
                LiveWindowActivity::class.java)
            intent.putExtra("mediacodec",mediacodec)
            context.startActivity(intent)
        }
    }

    private fun parseIntent(){
        mediacodec = intent.getIntExtra("mediacodec",0)
    }

    private fun initView(){
        videoView = findViewById(R.id.video)
        videoView?.setMediacodec(mediacodec?:0)
        videoView?.setVideoListener(this)
        videoView?.setPath("rtsp://192.168.0.10:8554/H264Video")
        try {
            videoView?.load()
        } catch (e: IOException) {
            Toast.makeText(this, "播放失败", Toast.LENGTH_SHORT)
            e.printStackTrace()
        }

    }


    override fun onSeekComplete(p0: IMediaPlayer?) {
    }

    override fun onInfo(p0: IMediaPlayer?, p1: Int, p2: Int): Boolean {
        return false
    }

    override fun onVideoSizeChanged(p0: IMediaPlayer?, p1: Int, p2: Int, p3: Int, p4: Int) {
    }

    override fun onBufferingUpdate(p0: IMediaPlayer?, p1: Int) {
    }

    override fun onPrepared(p0: IMediaPlayer?) {
        videoView?.start()
    }

    override fun onCompletion(p0: IMediaPlayer?) {
    }

    override fun onError(p0: IMediaPlayer?, p1: Int, p2: Int): Boolean {
        if(p1 == -10000 && p2 == 0){
            videoView?.load()
        }
        return false
    }

    override fun onDestroy() {
        super.onDestroy()
        videoView?.stop()
    }
}